package waal.pc.frontend.config;

import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

import waal.pc.backend.module.Module;

public class ModuleListCellRenderer extends DefaultListCellRenderer {

	private static final long serialVersionUID = 1L;
	private Border border;
	private ImageIcon[] smallIcons;
	
	public ModuleListCellRenderer() {
		this.border = new LineBorder(new Color(0,128,255), 2);
		this.smallIcons = new ImageIcon[8];

		this.smallIcons[0] = createImageIcon("/waal/pc/frontend/images/Ain.png",	"Analog Input");
		this.smallIcons[1] = createImageIcon("/waal/pc/frontend/images/Aout.png",	"Analog Output");
		this.smallIcons[2] = createImageIcon("/waal/pc/frontend/images/Ainout.png",	"Analog Mixed");
		this.smallIcons[3] = createImageIcon("/waal/pc/frontend/images/Din.png",	"Digital Input");
		this.smallIcons[4] = createImageIcon("/waal/pc/frontend/images/Dout.png",	"Digital Output");
		this.smallIcons[5] = createImageIcon("/waal/pc/frontend/images/Dinout.png",	"Digital Mixed");
		this.smallIcons[6] = createImageIcon("/waal/pc/frontend/images/Anone.png",	"Analog");
		this.smallIcons[7] = createImageIcon("/waal/pc/frontend/images/Dnone.png",	"Digital");
	}
	
	@Override
	public Component getListCellRendererComponent(JList<? extends Object> list,
			Object value, int index, boolean isSelected, boolean cellHasFocus) {
		
		Module module = (Module) value;
		JLabel l;
		//l.setOpaque(true);
		
		boolean input = module.hasInput();
		boolean output = module.hasOutput();
		if(module.isDigital()) {
			if(module.isInitilized()) {
				if(input && output){
					l = new JLabel(module.getName(), this.smallIcons[5], SwingConstants.LEFT);
				} else if(input) {
					l = new JLabel(module.getName(), this.smallIcons[3], SwingConstants.LEFT);
				} else {
					l = new JLabel(module.getName(), this.smallIcons[4], SwingConstants.LEFT);
				}
			} else {
				l = new JLabel(module.getName(), this.smallIcons[7], SwingConstants.LEFT);
			}
		} else {
			if(module.isInitilized()) {
				if(input && output){
					l = new JLabel(module.getName(), this.smallIcons[2], SwingConstants.LEFT);
				} else if(input) {
					l = new JLabel(module.getName(), this.smallIcons[0], SwingConstants.LEFT);
				} else {
					l = new JLabel(module.getName(), this.smallIcons[1], SwingConstants.LEFT);
				}
			} else {
				l = new JLabel(module.getName(), this.smallIcons[6], SwingConstants.LEFT);
			}
		}
		if(!module.isActive()) {
			l.setForeground(Color.GRAY);
		}

		if(isSelected) {
			l.setBorder(border);
		} 
			
		return l;
	}

	protected ImageIcon createImageIcon(String path, String description) {
		java.net.URL imgURL = getClass().getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL, description);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}
	
	
}
